<template>
  <div>
    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        <el-form-item label="账户名称">
          <el-input v-model="searchInfo.account" placeholder="搜索条件" />
        </el-form-item>
        <el-form-item label="用户姓名">
          <el-input v-model="searchInfo.name" placeholder="搜索条件" />
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          <el-button size="mini" type="primary" icon="el-icon-plus" @click="openDialog">新增</el-button>
          <el-popover v-model="deleteVisible" placement="top" width="160">
            <p>确定要删除吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="deleteVisible = false">取消</el-button>
              <el-button size="mini" type="primary" @click="onDelete">确定</el-button>
            </div>
            <el-button slot="reference" icon="el-icon-delete" size="mini" type="danger" style="margin-left: 10px;">批量删除</el-button>
          </el-popover>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="multipleTable"
      border
      stripe
      style="width: 100%"
      tooltip-effect="dark"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="日期" width="180">
        <template slot-scope="scope">{{ scope.row.CreatedAt|formatDate }}</template>
      </el-table-column>
      <el-table-column label="账户名称" prop="account" width="120" />
      <el-table-column label="职业身份" prop="career" width="120" />
      <el-table-column label="地理位置" prop="location" width="120" />
      <el-table-column label="用户姓名" prop="name" width="120" />
      <el-table-column label="社交平台" prop="plateForm" width="120">
        <template slot-scope="scope">
          {{ filterDict(scope.row.plateForm,"Platform") }}
        </template>
      </el-table-column>
      <el-table-column label="性别" prop="sex" width="120">
        <template slot-scope="scope">
          {{ filterDict(scope.row.sex,"sex") }}
        </template>
      </el-table-column>
      <el-table-column label="请求状态" prop="reqState" width="120">
        <template slot-scope="scope">
          {{ filterDict(scope.row.reqState,"reqState") }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.reqState === 0" size="small" type="primary" icon="el-icon el-icon-view" class="table-button" @click="sendModelRequest('mindScale', scope.row.ID)">发起请求</el-button>
          <el-button v-if="scope.row.reqState === 2" size="small" type="primary" icon="el-icon el-icon-view" class="table-button" @click="goMindScaler(scope.row.ID)">查看结果</el-button>
          <el-button size="small" type="primary" icon="el-icon-edit" class="table-button" @click="updateMindScale(scope.row)">变更</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-dialog :before-close="closeDialog" :visible.sync="dialogFormVisible" :title="type === 'create'? '新增监听账户':'修正记录'">
      <el-form :model="formData" label-position="right" label-width="80px">
        <div v-if="type === 'create'">
          <el-form-item label="账户名称:">
            <el-input v-model="formData.account" clearable placeholder="请输入" />
          </el-form-item>

          <el-form-item label="社交平台:">
            <el-select v-model="formData.plateForm" placeholder="请选择" clearable>
              <el-option v-for="(item,key) in PlatformOptions" :key="key" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="账号头像:">
            <el-input v-model="formData.avatar" clearable placeholder="请输入" />
          </el-form-item>

          <el-form-item label="职业身份:">
            <el-input v-model="formData.career" clearable placeholder="请输入" />
          </el-form-item>

          <el-form-item label="地理位置:">
            <el-input v-model="formData.location" clearable placeholder="请输入" />
          </el-form-item>

          <el-form-item label="用户姓名:">
            <el-input v-model="formData.name" clearable placeholder="请输入" />
          </el-form-item>

          <el-form-item label="性别:">
            <el-select v-model="formData.sex" placeholder="请选择" clearable>
              <el-option v-for="(item,key) in sexOptions" :key="key" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="enterDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  deleteMindScale,
  deleteMindScaleByIds,
  updateMindScale,
  findMindScale,
  getMindScaleList
} from '@/api/mind_scale' //  此处请自行替换地址
import { formatTimeToStr } from '@/utils/date'
import infoList from '@/mixins/infoList'
export default {
  name: 'MindScale',
  filters: {
    formatDate: function(time) {
      if (time !== null && time !== '') {
        var date = new Date(time)
        return formatTimeToStr(date, 'yyyy-MM-dd hh:mm:ss')
      } else {
        return ''
      }
    },
    formatBoolean: function(bool) {
      if (bool != null) {
        return bool ? '是' : '否'
      } else {
        return ''
      }
    }
  },
  mixins: [infoList],
  data() {
    return {
      listApi: getMindScaleList,
      dialogFormVisible: false,
      type: '',
      deleteVisible: false,
      multipleSelection: [],

      PlatformOptions: [],

      sexOptions: [],

      reqStateOptions: [],

      formData: {
        account: '',
        avatar: '',
        career: '',
        location: '',
        name: '',
        selfIntroduction: ''
      }
    }
  },
  async created() {
    await this.getTableData()

    await this.getDict('Platform')

    await this.getDict('sex')
    await this.getDict('reqState')
  },
  methods: {
  // 条件搜索前端看此方法
    onSubmit() {
      this.page = 1
      this.pageSize = 10
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteRow(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteMindScale(row)
      })
    },
    async onDelete() {
      const ids = []
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      this.multipleSelection &&
        this.multipleSelection.map(item => {
          ids.push(item.ID)
        })
      const res = await deleteMindScaleByIds({ ids })
      if (res.code === 0) {
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        if (this.tableData.length === ids.length && this.page > 1) {
          this.page--
        }
        this.deleteVisible = false
        this.getTableData()
      }
    },
    async updateMindScale(row) {
      const res = await findMindScale({ ID: row.ID })
      this.type = 'update'
      if (res.code === 0) {
        this.formData = res.data
        this.dialogFormVisible = true
      }
    },
    closeDialog() {
      this.dialogFormVisible = false
      this.formData = {
        account: '',
        avatar: '',
        career: '',
        location: '',
        name: '',
        plateForm: 1,
        selfIntroduction: '',
        sex: 1

      }
    },
    async deleteMindScale(row) {
      const res = await deleteMindScale({ ID: row.ID })
      if (res.code === 0) {
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        if (this.tableData.length === 1 && this.page > 1) {
          this.page--
        }
        this.getTableData()
      }
    },
    async enterDialog() {
      let res = 0
      switch (this.type) {
        case 'create':
          // TODO 发起爬虫请求
          this.$message({
            type: 'info',
            message: '发起爬虫请求'
          })
          break
        case 'update':
          res = await updateMindScale(this.formData)
          break
        default:
          console.log('创建监听账号消息')
          break
      }
      if (res.code === 0) {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.closeDialog()
        this.getTableData()
      }
    },
    openDialog() {
      this.type = 'create'
      this.dialogFormVisible = true
    },
    goMindScaler(id) {
      const name = 'scalerPerson'
      const query = { id }
      this.$router.push({ name, query })
    }
  }
}
</script>

<style>
</style>
